//
//  UserEditProfessionalController.m
//  CheekiOS
//
//  Created by mac on 2017/7/5.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserEditProfessionalController.h"
#import "HSLimitText.h"
#import "UserEditProfessionalCell.h"

@interface UserEditProfessionalController ()<HSLimitTextDelegate,UserEditProfessionalCellDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HSLimitText *ProfessText;  ///< textField
@property (nonatomic, strong) NSArray *titleArr;


@end

@implementation UserEditProfessionalController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self selfTitleFontColor];
    self.title = @"职业";
    
    [self requestDate];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *LeftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [LeftBtn addTarget:self action:@selector(LeftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [LeftBtn setTitle:@"取消" forState:UIControlStateNormal];
    LeftBtn.frame = CGRectMake(0, 0, 40, 40);
    [LeftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    LeftBtn.titleLabel.font = FontP(15);
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:LeftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;

    
    UIButton *RightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [RightBtn addTarget:self action:@selector(RightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [RightBtn setTitle:@"保存" forState:UIControlStateNormal];
    RightBtn.frame = CGRectMake(0, 0, 40, 40);
    [RightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    RightBtn.titleLabel.font = FontP(15);
    UIBarButtonItem *RightItem = [[UIBarButtonItem alloc]initWithCustomView:RightBtn];
    self.navigationItem.rightBarButtonItem = RightItem;
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 65)];
    
    bgView.backgroundColor = RGB(246, 246, 246);
    
    
    
    self.ProfessText = [[HSLimitText alloc] init];
    self.ProfessText.type =TextInputTypeTextfield;
    self.ProfessText.delegate = self;
    self.ProfessText.placeholder = @"选择下面或自填";
    self.ProfessText.maxLength = 6;
    self.ProfessText.isBecomFirstResponder = NO;
    self.ProfessText.frame = CGRectMake(0, 10, KScreenWidth, 55);
    self.ProfessText.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:self.ProfessText];
    


    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView = bgView;
    
    [self.view addSubview:self.tableView];
    
    _titleArr = [[NSArray alloc] init];

    
//    _titleArr = @[@"互联网从业者",@"职业经理人",@"私营企业主",@"中层管理者",@"大学生",@"公司职员",@"工程师",@"军人",@"警察",@"医生",@"护士",@"空乘",@"演员",@"歌手",@"模特",@"教师",@"设计师",@"导演",@"国企工作者",@"机关工作者",@"媒体工作者"];
    
    

}

- (void)requestDate{
    
    [HcardHttpmanager getProfessionalSuccess:^(id result) {
        NSString *str = result;
        if (str.length) {
            
            _titleArr = [str componentsSeparatedByString:@","];
        }
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)buttonActionClick:(UIButton *)button{
    
    self.ProfessText.text = button.titleLabel.text;


}


- (void)LeftBtnAction{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

//上传保存
- (void)RightBtnAction{
    
    NSDictionary *dict =[NSDictionary dictionary];
    dict = @{@"uid":[HQHttpManager getUserId],
             @"job":self.ProfessText.text
             };

    [HcardHttpmanager MakePersionalInfoWithParam:dict success:^(id sucess) {
        if(sucess){
            NSLog(@"设置成功");
            [SVProgressHUD showWithStatus:@"上传成功"];
            
            [SVProgressHUD dismissWithDelay:0.8];
            
            
            NSString *Str = @"NotificationUserCoverimageUpdate";
            [[NSNotificationCenter defaultCenter] postNotificationName:Str object:nil
             ];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError *error) {
        
    }];

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserEditProfessionalCell *cell = [UserEditProfessionalCell cellWithTableView:tableView indexPath:indexPath];
    cell.delegate = self;
    cell.titleArr = self.titleArr;
    cell.backgroundColor = RGB(246, 246, 246);
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return KScreenHeight;
}

- (void)limitTextLimitInputOverStop:(HSLimitText *)textLimitInput{

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"超出限制，最多能输入6个字" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertVC addAction:confirAction];
    [self.navigationController presentViewController:alertVC animated:YES completion:nil];
    
}


#pragma mark ==========输入代理==========
- (void)limitTextLimitInput:(HSLimitText *)textLimitInput text:(NSString *)text
{
    self.ProfessText.text = text;
    
    
}


@end
